package com.cristina.attention.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.cristina.attention.base.BaseEntity;
import com.cristina.attention.utils.CoreUtils;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.CascadeType;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

/**
 * 群
 * @author by Attention
 * @Classname Group
 * @Date 2021/12/5
 */
@Data
@EqualsAndHashCode(callSuper = false)
@JsonInclude(JsonInclude.Include.NON_NULL)
@TableName(value = "im_group")
public class Group extends BaseEntity {

    @TableId(type = IdType.ASSIGN_ID)
    @TableField(value = "group_id")
    private Long groupId;

    @TableField(value = "group_name")
    private String groupName;

    /**
     * 群描述
     */
    @TableField(value = "description")
    private String description;

    @TableField(value = "owner_id")
    private Long ownerId;

    /**
     * 一个群对应一个群主
     */
    @OneToOne(optional = false, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "ownerId")
    @TableField(exist = false)
    private User owner;

    @TableField(value = "group_avatar")
    private String groupAvatar;

    @TableField(value = "group_size")
    private Integer groupSize;

    /**
     * 群公告
     */
    @TableField(value = "announcement")
    private String announcement;

    @TableField(value = "id")
    private int id;

    @TableField(value = "`index`")
    private int index;

    public static Group initGroup(Long groupOwnerId) {
        Group group = new Group();
        int code = CoreUtils.buildCacheKeyBusinessCode(String.valueOf(groupOwnerId));
        group.setId(code);
        group.setIndex(code);
        group.setOwnerId(groupOwnerId);
        return group;
    }

}
